Skip to content

Speed up flake8-async by telling libcst to skip deepcopy.#438

Merged
jakkdl merged 2 commits intopython-trio:mainfrom
yilei:libcst-optimization
Apr 6, 2026
Merged

Speed up flake8-async by telling libcst to skip deepcopy.#438
jakkdl merged 2 commits intopython-trio:mainfrom
yilei:libcst-optimization

Conversation

@yilei
Copy link
Copy Markdown
Contributor

@yilei yilei commented Apr 3, 2026

Why? The default deepcopy in libcst guards against the same CST node object appearing at two positions in the tree (metadata is keyed by node identity). In our use of libcst in flake8-async, the parser output and the result of a prior .visit() never share nodes, so the copy is wasted work. This stays safe as long as no visitor returns a cached CST node from multiple leave_* calls.

See pyref.dev/libcst.metadata.MetadataWrapper.__init__

Running the flake8-async tests on my machine now takes 29.81s instead of 38.95s with this change.

@jakkdl
Copy link
Copy Markdown
Member

jakkdl commented Apr 6, 2026

Good catch! This does seem to introduce a small footgun in development, but as far as I know none of the current visitors duplicates any nodes so it should indeed be perfectly safe now and I think the speedup is worth the minor future risk.
Thank you for the PR!

@jakkdl jakkdl merged commit 038cba5 into python-trio:main Apr 6, 2026
10 checks passed
@trio-bot
Copy link
Copy Markdown

trio-bot bot commented Apr 6, 2026

Hey @yilei, it looks like that was the first time we merged one of your PRs! Thanks so much! 🎉 🎂

If you want to keep contributing, we'd love to have you. So, I just sent you an invitation to join the python-trio organization on Github! If you accept, then here's what will happen:

  • Github will automatically subscribe you to notifications on all our repositories. (But you can unsubscribe again if you don't want the spam.)

  • You'll be able to help us manage issues (add labels, close them, etc.)

  • You'll be able to review and merge other people's pull requests

  • You'll get a [member] badge next to your name when participating in the Trio repos, and you'll have the option of adding your name to our member's page and putting our icon on your Github profile (details)

If you want to read more, here's the relevant section in our contributing guide.

Alternatively, you're free to decline or ignore the invitation. You'll still be able to contribute as much or as little as you like, and I won't hassle you about joining again. But if you ever change your mind, just let us know and we'll send another invitation. We'd love to have you, but more importantly we want you to do whatever's best for you.

If you have any questions, well... I am just a humble Python script, so I probably can't help. But please do post a comment here, or in our chat, or on our forum, whatever's easiest, and someone will help you out!

@yilei yilei deleted the libcst-optimization branch April 6, 2026 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants